package com.woniuxy.login.service.impl;

import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.login.dao.LoginDao;
import com.woniuxy.login.entity.Administrator;
import com.woniuxy.login.service.LoginService;
import com.woniuxy.login.utils.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Waves
 * @date 2022/6/14 14:27
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginDao loginDao;

    @Override
    public ResponseResult findByUsername(String username,String password) {
        if(loginDao.findByUsername(username) != null){
            //存在该用户，校验密码
            Administrator administrator = loginDao.findByUsername(username);
            String salt = administrator.getSalt();
            String encryptPassword = MD5Util.encryptPassword(password, salt);
            if(administrator.getPassword().equals(encryptPassword)){
                return new ResponseResult(
                        200,
                        "查询成功",
                        administrator,
                        ResStatus.SUCCESS
                );
            }else{
                return new ResponseResult(
                        401,
                        "密码错误",
                        null,
                        ResStatus.FAIL
                );
            }
        }
        return new ResponseResult(
                402,
                "用户不存在",
                null,
                ResStatus.FAIL
        );
    }
}
